package weaver.interfaces.solead.utils;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import weaver.general.BaseBean;

/**
 * @Title: ecology-idea
 * @Company: 泛微软件
 * @author: weaver.Dusongsong
 * @version: 1.0
 * @CreateDate: 2022-06-06
 * @Description: ${description}
 * @ModifyLog:
 **/

public class SoleadUtil {

    /**
     * 组装XML，订单下达
     */
    public static String Ddxd_XMLContent(JSONObject header, JSONArray bodyList) {
        String result = "";
        JSONObject headxml = new JSONObject();
        JSONObject Documents = new JSONObject();
        BaseBean baseBean = new BaseBean();
        baseBean.writeLog( ">>>>XMLContent>>>>" );
        baseBean.writeLog( ">>>>XMLContent>>>>"+bodyList );
        baseBean.writeLog( ">>>>XMLContent>>>>"+bodyList.size() );
        try {
            if (!header.isEmpty()) {
                Documents.put( "row", header );
                headxml.put( "Documents", Documents );
                baseBean.writeLog( ">>>>headxml>>>>"+headxml.toJSONString() );
            }
            String head = Json2XmlUtils.JsonToXml( headxml );

            String Document_Lines = "";
            if (bodyList.size() > 0) {
                for (int i = 0; i < bodyList.size(); i++) {
                    JSONObject row = new JSONObject();
                    row.put( "row", bodyList.getJSONObject( i ) );
                    String rowxml = Json2XmlUtils.JsonToXml( row ).substring( 39 );
                    baseBean.writeLog( ">>>>row>>>>"+row.toJSONString() );
                    Document_Lines +="".equals(Document_Lines)?rowxml:""+rowxml;

                }
            }
            result = head.substring( 39 ) + "<Document_Lines>"+Document_Lines+"</Document_Lines>";
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }


    /**
     * 组装XML，采购订单
     */
    public static String Cgdd_XMLContent(JSONObject header, JSONArray bodyList) {
        String result = "";
        JSONObject headxml = new JSONObject();
        JSONObject Documents = new JSONObject();
        BaseBean baseBean = new BaseBean();
        baseBean.writeLog( ">>>>XMLContent>>>>" );
        try {
            if (!header.isEmpty()) {
                Documents.put( "row", header );
                headxml.put( "Documents", Documents );
                baseBean.writeLog( ">>>>headxml>>>>"+headxml.toJSONString() );
            }
            String head = Json2XmlUtils.JsonToXml( headxml );

            String Document_Lines = "";
            if (bodyList.size() > 0) {
                for (int i = 0; i < bodyList.size(); i++) {
                    JSONObject row = new JSONObject();
                    row.put( "row", bodyList.getJSONObject( i ) );
                    String rowxml = Json2XmlUtils.JsonToXml( row ).substring( 39 );
                    baseBean.writeLog( ">>>>row>>>>"+row.toJSONString() );
                    Document_Lines +="".equals(Document_Lines)?rowxml:""+rowxml;
                }
            }
            result = head.substring( 39 ) + "<Document_Lines>"+Document_Lines+"</Document_Lines>";
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

    /**
     * 组装XML，业务伙伴
     */
    public static String ywhb_XMLContent(JSONObject headerParam, JSONArray bodyList1Param,JSONArray bodyList2Param) {
        String result = "";
        BaseBean baseBean = new BaseBean();
        baseBean.writeLog( ">>>>ywhb_XMLContent>>>>" );
        try {
            StringBuilder header = new StringBuilder( "<BusinessPartners>" );
            if (!headerParam.isEmpty()) {
                JSONObject row = new JSONObject();
                row.put( "row", headerParam );
                String headerXML = Json2XmlUtils.JsonToXml( row ).substring( 39 );
                header.append( headerXML );
            }
            header.append("</BusinessPartners>");
            baseBean.writeLog( ">>>>header>>>>"+header);

            StringBuilder body1 = new StringBuilder( "<ContactEmployees>" );
            if (bodyList1Param.size() > 0) {
                for (int i = 0; i < bodyList1Param.size(); i++) {
                    JSONObject row = new JSONObject();
                    row.put( "row", bodyList1Param.getJSONObject( i ) );
                    String body1XML = Json2XmlUtils.JsonToXml( row ).substring( 39 );
                    body1.append( body1XML );
                }
            }
            body1.append("</ContactEmployees>");
            baseBean.writeLog( ">>>>ContactEmployees>>>>"+body1);

            StringBuilder body2 = new StringBuilder( "<BPAddresses>" );
            if (bodyList2Param.size() > 0) {
                for (int i = 0; i < bodyList2Param.size(); i++) {
                    JSONObject row = new JSONObject();
                    row.put( "row", bodyList2Param.getJSONObject( i ) );
                    String body2XML = Json2XmlUtils.JsonToXml( row ).substring( 39 );
                    body2.append( body2XML );
                }
            }
            body2.append("</BPAddresses>");
            baseBean.writeLog( ">>>>BPAddresses>>>>"+body2);

            result = header + body1.toString()+body2.toString();
            baseBean.writeLog( ">>>>result>>>>"+result);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }
    /**
    *@Description: 商贸销售
    *@Param: [header, bodyList]
    *@return: java.lang.String
    *@Author: Dusongsong
    *@Date: 2022/8/31
    */
    public static String SMXS_XMLContent(JSONObject header, JSONArray bodyList) {
        String result = "";
        JSONObject headxml = new JSONObject();
        JSONObject Documents = new JSONObject();
        BaseBean baseBean = new BaseBean();
        baseBean.writeLog( ">>>>XMLContent>>>>" );
        try {
            if (!header.isEmpty()) {
                Documents.put( "row", header );
                headxml.put( "Documents", Documents );
                baseBean.writeLog( ">>>>headxml>>>>"+headxml.toJSONString() );
            }
            String head = Json2XmlUtils.JsonToXml( headxml );
            String Document_Lines = "";
            if (bodyList.size() > 0) {
                for (int i = 0; i < bodyList.size(); i++) {
                    JSONObject row = new JSONObject();
                    row.put( "row", bodyList.getJSONObject( i ) );
                    String rowxml = Json2XmlUtils.JsonToXml( row ).substring( 39 );
                    baseBean.writeLog( ">>>>row>>>>"+row.toJSONString() );
                    Document_Lines +="".equals(Document_Lines)?rowxml:""+rowxml;
                }
            }
            result = head.substring( 39 ) + "<Document_Lines>"+Document_Lines+"</Document_Lines>";
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }
}
